package com.tqk.taxismart.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.tqk.constant.Constant;
import com.tqk.taxismart.MainActivity;
import com.tqk.taxismart.R;
import com.tqk.taxismart.obj.Taxi;

public class ListTaxi extends Activity implements OnClickListener {
    ListView listView;
    List<Taxi> data;
    String TAG = "ListTaxi";
    TaxiAdapter adapter;
    Button btnSortByName, btnSortByRating, btnSortByPrice;
    Button btnBack;

    ListTaxiInterface handler = new ListTaxiInterface() {

	@Override
	public void actionCallNumber(String number) {
	    CallNumber(number);
	}

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.layout_list_taxi);
	listView = (ListView) findViewById(R.id.listTaxi_id);
	btnSortByName = (Button) findViewById(R.id.btnSortByName_id);
	btnSortByRating = (Button) findViewById(R.id.btnSortByRate_id);
	btnSortByPrice = (Button) findViewById(R.id.btnSortByMoney_id);
	btnBack = (Button) findViewById(R.id.btnBack_id);
	btnBack.setOnClickListener(this);
	btnSortByName.setOnClickListener(this);
	btnSortByPrice.setOnClickListener(this);
	btnSortByRating.setOnClickListener(this);

	data = new ArrayList<Taxi>();
	data = parserData();
	Constant.DATA_TAXI = data;
	// set adapter for list
	adapter = new TaxiAdapter(ListTaxi.this, data, handler);
	listView.setAdapter(adapter);
    }

    private List<Taxi> parserData() {
	String dataJson = Constant.readFileJson(getApplicationContext(),
		"data/list_taxi.json");

	try {
	    JSONObject jsObj = new JSONObject(dataJson);
	    if (jsObj != null) {
		JSONArray jsArray = jsObj.getJSONArray("data");
		Log.i(TAG, "leng data = " + jsArray.length());
		for (int i = 0; i < jsArray.length(); i++) {
		    JSONObject obj = jsArray.getJSONObject(i);
		    String name = obj.getString("name_taxi");
		    String seatCount = obj.getString("seat_count");
		    String number = obj.getString("number");
		    float price1 = Float.valueOf(obj.getString("price1"));
		    float price2 = Float.valueOf(obj.getString("price2"));
		    float price3 = Float.valueOf(obj.getString("price3"));
		    int rateCount = obj.getInt("rate");
		    Taxi t = new Taxi();
		    t.setNameTaxi(name);
		    t.setseatCount(seatCount);
		    t.setCallLine(number);
		    t.setRateCount(rateCount);
		    t.setPrice(price1);
		    t.setPrice2(price2);
		    t.setPrice3(price3);
		    data.add(t);
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return data;
    }

    private void CallNumber(String number) {
	Intent callIntent = new Intent(Intent.ACTION_CALL);
	callIntent.setData(Uri.parse("tel:" + number.toString()));
	callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	startActivity(callIntent);
    }

    @Override
    public void onClick(View v) {
	int id = v.getId();

	switch (id) {
	case R.id.btnSortByName_id:
	    sortByName();
	    adapter.notifyDataSetChanged();
	    listView.setAdapter(adapter);
	    for (int i = 0; i < data.size(); i++) {
		System.out.println("Name = " + data.get(i).getNameTaxi());
	    }
	    break;
	case R.id.btnSortByRate_id:
	    sortByRating();
	    Collections.reverse(data);
	    adapter.notifyDataSetChanged();
	    listView.setAdapter(adapter);
	    break;
	case R.id.btnSortByMoney_id:
	    sortByPrice();
	    adapter.notifyDataSetChanged();
	    listView.setAdapter(adapter);
	    break;
	case R.id.btnBack_id:
	    Intent intentBack = new Intent(getApplicationContext(),
		    MainActivity.class);
	    startActivity(intentBack);
	    finish();
	    break;
	default:
	    break;
	}
    }

    private void sortByRating() {
	if (data.size() > 0) {
	    Collections.sort(data, new Comparator<Taxi>() {

		@Override
		public int compare(Taxi t1, Taxi t2) {

		    return t1.getRateCount() > t2.getRateCount() ? 1 : -1;
		}

	    });
	}
    }

    private void sortByName() {
	if (data.size() > 0) {
	    Collections.sort(data, new Comparator<Taxi>() {

		@Override
		public int compare(Taxi t1, Taxi t2) {
		   
		    return t1.getNameTaxi().compareToIgnoreCase(
			    t2.getNameTaxi());
		}

	    });
	}
    }

    private void sortByPrice() {
	if (data.size() > 0) {
	    Collections.sort(data, new Comparator<Taxi>() {

		@Override
		public int compare(Taxi t1, Taxi t2) {
		    int ok = 0;
//		    return t1.getPrice2() < t2.getPrice2() ? -1 : 1;
		    if (t1.getPrice2() < t2.getPrice2()) {
			return ok = 1;
		    }
		    if (t1.getPrice2() > t2.getPrice2()) {
			return ok = -1;
		    }
		    return ok;
		}

	    });
	}
    }
}
